package com.ultrawise.survey.module.surevybase.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.service.TSurveyDataAnswerService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoQuestionService;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoQuestion;

@Controller
@RequestMapping(value = "/symanage/question")
public class TSurveyInfoQuestionControl extends BaseContoller {
	@Autowired
	TSurveyInfoQuestionService tSurveyInfoQuestionService;
	@Autowired
	TSurveyDataAnswerService tSurveyDataAnswerService;

	public TSurveyInfoQuestionControl() {

	}

	/****
	 * 添加问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addquestion.htm")
	public ModelAndView addquestion(TSurveyInfoQuestion tquestion, HttpServletRequest request) {

		try {
			boolean isadd = false; //判断是增加还是编辑操作
			if (tquestion == null || tquestion.getId() == null) {
				Integer tid = (Integer) tSurveyInfoQuestionService.getMaxID(tquestion);
				tid = (tid == null) ? 1 : tid + 1;
				tquestion.setId(tid);
				isadd = true;

				Integer ordnum = (Integer) tSurveyInfoQuestionService.getMaxByColname(tquestion, "QN_ORDER");
				ordnum = (ordnum == null) ? 1 : ordnum + 1;
				tquestion.setQn_order(ordnum);
			}
			// 追加选项信息
			if (tquestion.getQn_options() != null) {
				String temp = tquestion.getQn_options().replaceAll("\r\n",SurveyConstants.SURVEY_DATAINFO_SPACE);
				//	temp.replaceAll(SurveyConstants.SURVEY_DATAINFO_SPACE, );
				tquestion.setQn_options(temp);
			}

			if (isadd) {
				// 保存问题信息
				tSurveyInfoQuestionService.saveObj(tquestion);
			} else {
				// 编辑问题信息
				tSurveyInfoQuestionService.updateObj(tquestion);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "系统错误，请和管理员联系。");
		}
		request.setAttribute("message", "您已经成功保存问题的相关信息。");
		request.setAttribute("url",
				request.getContextPath() + "/symanage/question/question.htm?sy_id=" + tquestion.getSy_id());
		return new ModelAndView("/massage");
	}

	/*****
	 * 确认删除问题信息
	 * 
	 * @param tquestion
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delequestion.htm")
	public ModelAndView delequestion(TSurveyInfoQuestion tquestion, HttpServletRequest request) {

		try {
			tSurveyDataAnswerService.deleAnswerByQuestionId(tquestion.getId());			
			tSurveyInfoQuestionService.removeObj(tquestion.getId(), tquestion);
			
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "刪除异常，请与管理员联系。");
		}
		request.setAttribute("message", "您已经成功删除问题信息。");
		request.setAttribute("url",
				request.getContextPath() + "/symanage/question/question.htm?sy_id=" + tquestion.getSy_id());
		return new ModelAndView("/massage");
	}

	/****
	 * 查看问题详细信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "question.htm")
	public ModelAndView question(TSurveyInfoQuestion question, ModelMap map, HttpServletRequest request) {
		Integer syid = question.getSy_id();
		List<TSurveyInfoQuestion> lists = tSurveyInfoQuestionService.getQuestionBysyid(syid);

		TSurveyInfoBase tbase = new TSurveyInfoBase();
		TSurveyInfoQuestion tquestion = new TSurveyInfoQuestion();
		try {
			tbase = (TSurveyInfoBase) tSurveyInfoQuestionService.queryByID(question.getSy_id(), new TSurveyInfoBase(),
					TSurveyInfoBase.class);
			if (question != null && question.getId() != null) {
				tquestion = (TSurveyInfoQuestion) tSurveyInfoQuestionService.queryByID(question.getId(),
						new TSurveyInfoQuestion(), TSurveyInfoQuestion.class);
				if (tquestion != null && tquestion.getQn_options() != null) {
					tquestion.setQn_options(tquestion.getQn_options().replaceAll(SurveyConstants.SURVEY_DATAINFO_SPACE,
							"\r\n"));
				}
			}
		} catch (ServiceException e) {
			
			e.printStackTrace();
		}
		map.put("tbase", tbase);
		map.put("tquestion", tquestion);
		map.put("list", lists);
		return new ModelAndView("/question/qlist");
	}
	
	@RequestMapping(value = "starbase.htm")
	public ModelAndView starbase(TSurveyInfoQuestion question,	HttpServletRequest request) {
		try {
			if (question != null && question.getId() != null) {
				question = (TSurveyInfoQuestion) tSurveyInfoQuestionService.queryByID(
						question.getId(), question, TSurveyInfoQuestion.class);
				if(question.getQn_isstop()!=null && question.getQn_isstop().equals(new Integer(0)))
					question.setQn_isstop(1);// 修改状态为开启状态
				else
					question.setQn_isstop(0);
				tSurveyInfoQuestionService.updateObj(question);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "更新失败，请重试或者联系管理员。");
		}
		request.setAttribute("message", "状态已经修改。");
		request.setAttribute("url", request.getContextPath() + "/symanage/question/question.htm?sy_id="+question.getSy_id());
		return new ModelAndView("/massage");
	}
	
}
